package com.lxy.user.ai;/*
引入依赖包
最低SDK版本要求：facebody20191230的SDK版本需大于等于3.0.7。
可以在此仓库地址中引用最新版本SDK：https://mvnrepository.com/artifact/com.aliyun/facebody20191230
<!-- https://mvnrepository.com/artifact/com.aliyun/facebody20191230 -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>facebody20191230</artifactId>
    <version>${aliyun.facebody.version}</version>
</dependency>
*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.facebody20191230.models.CompareFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.teautil.Common;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;

public class CompareFace {
    public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new com.aliyun.facebody20191230.Client(config);
    }

//    public static void main(String[] args_) throws Exception {
//        // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html
//        // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
//        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
//        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
//        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
//        com.aliyun.facebody20191230.Client client = CompareFace.createClient(accessKeyId, accessKeySecret);
//        // 场景一，使用本地文件
////        InputStream inputStreamA = new FileInputStream(new File("/tmp/CompareFace-right.png"));
//        InputStream inputStreamA = new FileInputStream(new File("D:/图片/1.jpg"));
//        // 场景二，使用任意可访问的url
//        URL url = new URL("https://luoxiaoyang.oss-cn-beijing.aliyuncs.com/es-face/3.jpg");
//        InputStream inputStreamB = url.openConnection().getInputStream();
////        // TODO
////        InputStream inputStreamB = new FileInputStream(new File("D:/图片/3.jpg"));
//        com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest compareFaceAdvanceRequest = new com.aliyun.facebody20191230.models.CompareFaceAdvanceRequest()
//                .setImageURLAObject(inputStreamA)
//                .setImageURLBObject(inputStreamB);
//        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        try {
//            // 复制代码运行请自行打印 API 的返回值
//            CompareFaceResponse compareFaceResponse = client.compareFaceAdvance(compareFaceAdvanceRequest, runtime);
//            // 获取整体结果
//            String jsonString = Common.toJSONString(TeaModel.buildMap(compareFaceResponse));
//            //解析json
//            JSONObject jsonObject = JSONObject.parseObject(jsonString);
//            JSONObject body = jsonObject.getJSONObject("body");
//            JSONObject data  = body.getJSONObject("Data");
//            double confidence = data.getDoubleValue("Confidence");
//            System.out.println(confidence);
//        } catch (TeaException teaException) {
//            // 获取整体报错信息
//            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
//            // 获取单个字段
//            System.out.println(teaException.getCode());
//        }
//    }
}